<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<%@include file="../base/frameBase.jsp" %>
<body>
<div class="demoTable">
    搜索图文标题：
    <div class="layui-inline">
        <input class="layui-input" name="id" id="demoReload" autocomplete="off">
      
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
    <button class="layui-btn layui-btn-normal" data-type="reload">刷新页面</button>
</div>
<script type="text/html" id="barTool">
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">查看图文</a>
</script>
<script type="text/html" id="upStateTpl">
    <input type="checkbox" {{# if(d.up_state==1){ }} checked {{# } }}  value="{{d.id}}" name="close" lay-skin="switch" lay-text="置顶|取消" lay-filter="upSwitch">
</script>
<table class="layui-hide" id="LAY_table_user" lay-filter="course"></table>
<script>
    layui.use('table', function () {
        var table = layui.table;
        var form = layui.form;
        //方法级渲染
        table.render({
            elem: '#LAY_table_user'
            , url: '${ctx}/pageData/getPageListByUid'
            , cols: [[
                {field: 'page_title', title: '图文标题', width: 180}
                , {field: 'page_context', title: '内容缩略', width: 180}
                , {field: 'page_click', title: '阅读量', width: 100}
                // , {field: 'page_like', title: '点赞数', width: 100}
                , {field: 'createTime', title: '上传时间', width: 277}
                , {field: 'up_state', title: '是否置顶', width: 150,templet:'#upStateTpl',unresize:true	}
                , {fixed: 'right', title: '操作', width: 180, align: 'center', toolbar: '#barTool'}
            ]]
            , id: 'tableReload'
            , page: true
            , height: 500
        });
        //监听表格复选框选择
        table.on('checkbox(course)', function (obj) {
        });
        
        form.on('switch(upSwitch)', function(data){
            var pageId = data.value;
            var upState = 0;
            if(data.elem.checked){
            	  upState = 1;
	        }
            // 发起请求删除课程
            $.ajax({
                url: "${ctx}/pageData/upState?id="+pageId+"&upState="+upState,
                method: 'GET',
                contentType: 'application/json',
                data: JSON.stringify(data), // 以json字符串方式传递
                success: function (data) {
                    console.log(data)
                    if (data.code != '00000') {
                        layer.msg('失败');
                    } else {
                    	if(upState==1){
                    		layer.msg('置顶成功！');
                    	}else{
                    		layer.msg('已取消置顶！');
                    	}
                        // 刷新下视频数据表格
                        table.reload('tableReload', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                    }
                },
                error: function (data) {
                    alert("信息发表失败，请检查网络连接。");
                }
            })
	       
        });
        //监听工具条
        table.on('tool(course)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {
                layer.confirm('真的删除该课程吗，课程标题：' + data.course_title, function (index) {

                    // 发起请求删除课程
                    $.ajax({
                        url: '${ctx}/videoData/deleteCourse',
                        method: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify(data), // 以json字符串方式传递
                        success: function (data) {
                            console.log(data)
                            if (data.code != '00000') {
                                layer.msg('[删除失败]' + data.msg);
                            } else {
                                layer.msg("删除成功！");
                                // 刷新下视频数据表格
                                table.reload('tableReload', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                                obj.del();
                            }
                        },
                        error: function (data) {
                            alert("信息发表失败，请检查网络连接。");
                        }
                    })

                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                // 课程下视频编辑页面
                // layer.alert('编辑行：<br>' + JSON.stringify(data))
                window.location.assign(encodeURI("${ctx}/page/detail?pageId=" + data.id));
            }else if(obj.event === 'contentUp'){
            	//置顶功能
            }
        });
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        page_title: demoReload.val()
                    }
                });
                console.log("reload suc")
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
<%@include file="../base/frameFoot.jsp" %>